import re
import gdb

# This class contains a list of function names and regex some other class will
# do something with. It's mainly used by TargetDrivers and SCCDetectors
# to know which functions to skip.
# USED BY: TargetDriver, RobustoDetector.

class FunctionNameRegistry(object):
    def __init__(self, functions=None):
        self.functionNames = []
        self.functionRegex = []

        self.registerFunctions(functions)

    def registerFunctions(self, functions):
        if functions:
            if "names" in functions and functions["names"]:
                self.functionNames.extend(functions["names"])

            if "regex" in functions and functions["regex"]:
                for regex in functions["regex"]:
                    self.functionRegex.append(re.compile(regex))

    def isRegistered(self, functionName):
        result = False

        if functionName:
            result = functionName in self.functionNames or self.matchesRegex(functionName)

        return result

    def matchesRegex(self, functionName):
        for regex in self.functionRegex:
            if regex.match(functionName):
                return True

        return False
